package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.domain.Bills;
import com.it.domain.BillsNum;
import com.it.domain.Goods;
import com.it.domain.Goods_Details;
import com.it.service.BillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("billsContriller")
public class BillsContriller {
    @Autowired
    private BillsService billsService;

    /**
     * 查询所有bills账单
     * @param page
     * @param size
     * @param mv
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findAllBills.do")
    public ModelAndView FindAllBills(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                     @RequestParam(name = "size", required = true, defaultValue = "5") int size,
                                     ModelAndView mv, HttpServletRequest request, HttpServletResponse response){
        List<Bills> bills = billsService.FindAllBills(page, size);
        PageInfo pageInfo = new PageInfo(bills);
        mv.addObject("list_bills",pageInfo);
        mv.setViewName("bills_FindAll");
        return mv;
    }

    /**
     * 日期范围查询bills
     * @param page
     * @param size
     * @param mv
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findBillsDate.do")
    public ModelAndView FindBillsDate(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                     @RequestParam(name = "size", required = true, defaultValue = "5") int size,
                                     ModelAndView mv, HttpServletRequest request, HttpServletResponse response){

        try {
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            df.setLenient(false);

            Date date01 = df.parse(request.getParameter("date01"));
            Date date02 = df.parse(request.getParameter("date02"));

            List<Bills> billsDate = billsService.findBillsDate(date01, date02, page, size);
            PageInfo pageInfo = new PageInfo(billsDate);
            mv.addObject("list_billsdate",pageInfo);
            mv.setViewName("bills_Finddatelist");

        } catch (ParseException e) {
            String st = "查询失败！";
            mv.addObject("return_as", st);
            mv.setViewName("returnBack");
        }
        return mv;

    }

    /**
     * 账单交易总记
     * @param mv
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findBillsNum")
    public ModelAndView FindBillsNum(BillsNum billsNum, ModelAndView mv, HttpServletRequest request, HttpServletResponse response){
        List<Bills> bills = billsService.FindAllBills1();
        double numProgfit = 0; //总盈利
        int numBills = 0;//账单总数
        for (Bills bill :
                bills) {
            numProgfit+=bill.getProfit();
            numBills++;
        }
        billsNum.setNumBills(numBills);
        billsNum.setNumProgfit(numProgfit);

        mv.addObject("listnum",billsNum);
        mv.setViewName("bills_num");
        return mv;
    }

    /**
     * 按日期查询账单交易总记
     * @param billsNum
     * @param mv
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findBillsDateNum.do")
    public ModelAndView FindBillsDateNum(BillsNum billsNum, ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        df.setLenient(false);

        Date date01 = df.parse(request.getParameter("date01"));
        Date date02 = df.parse(request.getParameter("date02"));

        List<Bills> billsDate1 = billsService.findBillsDate1(date01, date02);

        double numProgfit = 0; //总盈利
        int numBills = 0;//账单总数
        for (Bills bill :
                billsDate1) {
            numProgfit+=bill.getProfit();
            numBills++;
        }
        billsNum.setNumBills(numBills);
        billsNum.setNumProgfit(numProgfit);

        mv.addObject("listnum",billsNum);
        mv.setViewName("bills_num");
        return mv;

    }

}
